using QFramework;
using UnityEngine;
namespace PlatformShoot
{
    public interface ICameraSystem : ISystem
    {
        void SetTarget(Transform target);

    }

    public class CameraSystem : AbstractSystem, ICameraSystem
    {
        private Transform mTarget;
        /// <summary>
        /// 临时的坐标 用来缓存需要的计算坐标
        /// </summary>
        private Vector3 mTempPos;
        /// <summary>
        /// 跟随的范围 上下左右四个极限长度
        /// </summary>
        private float minX = -100f, maxX = 100f, minY = -100f, maxY = 100f;
        private float mSmoothSpeed = 5f;
        protected override void OnInit()
        {
            PublicMono.Instance.OnLateUpdate += Update;
            mTempPos.z = -10;
        }
        public void SetTarget(Transform target)
        {
            mTarget = target;
        }

        private void Update()
        {
            if (mTarget == null || mTarget.Equals(null))
            {
                return;
            }
            mTempPos.x = Mathf.Clamp(mTarget.position.x, minX, maxX);
            mTempPos.y = Mathf.Clamp(mTarget.position.y, minY, maxY);
            var cam = Camera.main.transform;
            var sqr = (cam.position - mTempPos).sqrMagnitude;
            if (sqr < 0.01f) return;
            cam.localPosition = Vector3.Lerp(cam.position, mTempPos, mSmoothSpeed);

        }
    }
}